import api from '../../utils/api'
import {formatRichText} from '../../utils/util'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
var WSCoordinate = require('../../utils/WSCoordinate.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageBaseURL: app.imageBaseURL,
    imageResizeParams: app.imageResizeParams,
    content:'',
    id:'',
    gourl:''
  },
  onClickLeft(){
    wx.navigateBack({
      delta: 1,
    })
  },
  navhome(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  //收藏
  async collect(e){
    if( !api.isauth() ){
      wx.navigateTo({
        url: this.data.gourl
      })
      return
    }
    
    let data={}
    data['flag'] = 'theme'
    data['action'] = 'base_collect'
    data['id'] = e.currentTarget.dataset.id
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      Toast.success(message)
      this.getList(e.currentTarget.dataset.id)
    }else {
      Toast(message)
    }
  },
  //取消收藏
  async cancelCollect(e){
    let data={}
    data['flag'] = 'theme'
    data['action'] = 'base_uncollect'
    data['id'] = e.currentTarget.dataset.id
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      Toast.success(message)
      this.getList(e.currentTarget.dataset.id)
    }else {
      Toast(message)
    }
  },
  //信息
  async getList (id) {
    let data={}
    data['flag'] = 'theme'
    data['action'] = 'base_detail'
    data['id'] = id
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      let arr = result.url.split(',')
      let arrhttp = []
      arr.forEach((item,index) => {
        arrhttp.push(api.checkurl(item))
      })
      result.url = arrhttp;
      result.qcode = api.checkurl(result.qcode)
      result.description = formatRichText(result.description)
      this.setData({
        content:result,
      })
    }else {
      Toast(message)
    }
  },
  openmap(){
    let lat = parseFloat(this.data.content.latitude||0)
    let lng = parseFloat(this.data.content.longitude||0)
    let point = WSCoordinate.transformFromBaiduToGCJ(lat, lng);
    console.log(lat,lng)
    wx.openLocation({
      latitude:point.latitude,
      longitude:point.longitude,
      scale: 18
    }).then(res => console.log('res: ', res))
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let gourl = '/pages/login/index?url='+encodeURIComponent('/pages/index_theme_class_detail/index?id='+options.id)
    this.setData({id:options.id,gourl})
    this.getList(options.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getUser()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: this.data.content.name,
      path: `/pages/index_theme_class_detail/index?id=${this.data.id}`,
      success(res){
        console.log('转发成功' + res)
      },
      fail(res){
        console.log('转发失败' + res);
      }
    }
  }
})